{% extends 'admin/_layout.html' %}

{% block body %}
<div class="row">
    <div class="col-12 grid-margin stretch-card">
        <div class="card">
            <div class="row">
                <div class="col-md-4">
                    <div class="card-body">
                        {% for one in task_groups %}
                            <a href="/tasks_list?group_id={{one['id']}}" class="btn btn-primary">{{one['name']}}({{one['task_cnt']}})</a>
                        {% endfor %}
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-12 grid-margin stretch-card">
        <div class="card">
            <div class="row">
                <div class="col-md-4">
                    <div class="card-body">
                        <a href="/edit_task?back={{request.url}}" class="btn btn-outline-dark btn-fw">添加</a>
                    </div>
                </div>
                <div class="col-md-8">
                    <div class="card-body">
                        <form class="form-inline" style="float:right">
                            <label class=""></label>
                            <input type="text" class="form-control mb-2 mr-sm-2" id="" placeholder="关键字">
                            <button type="submit" class="btn btn-primary mb-2">搜索</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-12 grid-margin stretch-card">
        <div class="card">
            <div class="card-body">
                <h4 class="card-title">任务管理</h4>
                <p class="card-description">
                    任务管理 /<code>任务列表</code>
                </p>
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            {% for t in cfg %}
                            <th>{{t['label']}}</th>
                            {% endfor %}
                            <th style="width:15%;">管理</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for row in data %}
                        {% autoescape false %}
                        <tr>
                            {% for item in cfg %}
                            <td>
                                {{row[item['field']]}}
                            </td>
                            {% endfor %}
                            <td>
                                {% if row['run_count'] > 0 %}
                                    <button onclick="showLogDetail({{row['id']}})" class="btn btn-inverse-success btn-fw">最新日志</button>
                                    <a href="/tasks_log_list?id={{row['id']}}&back={{request.url}}" class="btn btn-inverse-info btn-fw">执行日志</a>
                                {% endif %}
                                <button onclick="testTask({{row['id']}})" class="btn btn-inverse-primary btn-fw">测试任务</button>
                                {% if 1 == row['status'] %}
                                    <button onclick="stopTask({{row['id']}})" class="btn btn-inverse-warning btn-fw">停止任务</button>
                                {% endif %}
                                <a href="/edit_task?id={{row['id']}}&back={{request.url}}" class="btn btn-inverse-success btn-fw">编辑查看</a>
                                {% if 2 == row['status'] %}
                                    <button onclick="startTask({{row['id']}})" class="btn btn-inverse-secondary btn-fw">启动任务</button>
                                    <button onclick="deleteMe({{row['id']}})" class="btn btn-inverse-danger btn-fw">删除</button>
                                {% endif %}
                            </td>
                        </tr>
                        {% endautoescape %}
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="js/sweetalert2.min.js"></script>
<script src="vendors/js/SwalAide.js"></script>
<script src="js/jquery.min.js"></script>
<script>
    function showLogDetail(id){
        $.get('/show_log_detail',{task_id:id},
            function (rsp) {
                SwalAide.showHtml('日志详情',rsp.data);
            }, 'json');
    }

    function testTask(id) {
        SwalAide.lock();
        $.get('/test_task',{id:id},
            function (rsp) {
                // console.log(rsp);
                SwalAide.showHtml('测试详情',rsp.result);
            }, 'json');
    }

    function deleteMe(id){
        SwalAide.confirm(function(){
            $.get('/delete_task',{id:id},
                function (rsp) {
                     SwalAide.toast(rsp.message,rsp.error,function(){
                        location.reload();
                     });
                }, 'json');
        });
    }

    function startTask(id){
        $.get('/start_task',{id:id},
            function (rsp) {
                SwalAide.toast(rsp.message,rsp.error,function(){
                    location.reload()
                });
            }, 'json');
    }

    function stopTask(id){
        $.get('/stop_task',{id:id},
            function (rsp) {
                SwalAide.toast(rsp.message,rsp.error,function(){
                    location.reload()
                });
            }, 'json');
    }
</script>
{% endblock %}